package leetcode.剑指offer.前40;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/21 23:31
 * @description：https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/
 */
public class 剑指_15_二进制中1的个数 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    /*
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            return Integer.bitCount(n);
        }
    }
     */

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int count = 0;
            for (int i = 0; i < 32; i++) {
                if (((1 << i) & n) != 0) {
                    count++;
                }
            }
            return count;
        }
    }

}
